package is202.hrms.web;

import is202.hrms.ejb.StudentEJB;
import is202.hrms.entity.Student;
import is202.hrms.entity.Modul;
import is202.hrms.web.View;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.EJBException;
import javax.enterprise.context.Conversation;
import javax.enterprise.context.ConversationScoped;
import javax.inject.Inject;
import javax.inject.Named;

/**
 * The backing bean for department.xhtml. The Named annotation marks it
 * as a CDI backing bean. The ConversationScoped annotation controls
 * the lifecycle of the bean. The lifetime of a ConversationScoped bean is
 * explicitly managed in the code.
 *
 * @author even
 */
@Named("studbean")
@ConversationScoped
public class StudentBean implements Serializable {

    // The @Inject annotation is a request to the server to
    // give us a Conversation object, and store it in conv
    @Inject private Conversation conv;
    @EJB StudentEJB studEjb;
    private Student student;
    private boolean updating;

    // Department properties - These properties will be copied to a Department
    // object, when the time comes to save the data in the database.
    private long studentNr;
    private String navn;
    private List<Modul> moduler;

    public StudentBean() {
    }

    /**
     * called when the user selects a department from the list in
     * departments.xhtml.
     */
    public void setStudentNr(long studentNr) {
        if (conv.isTransient()) {
            conv.begin();
        }

        Student stud = studEjb.find(studentNr);
        if (null == stud) {
            // we will get here if depNo is not a valid, or if
            // depNo is valid but there is no department with that depno
            updating = false;
            studentNr = 0;
            navn = "";
            moduler = new ArrayList<Modul>();
        }
        else {
            updating = true;
            this.studentNr = stud.getStudentNr();
            navn = stud.getNavn();
            moduler = stud.getModuler();
        }
    }

    public long getStudentNr() {
        return studentNr;
    }

    public String getNavn() {
        return navn;
    }

    public void setNavn(String navn) {
        this.navn = navn;
    }

    public List<Modul> getModuler() {
        return moduler;
    }

    public boolean isUpdating() {
        return updating;
    }

    private Student createStudObject() {
        // Create a department object containing the data
        // from the form. The object can be passed on to
        // depListBean
        Student stud = new Student();
        stud.setStudentNr(studentNr);
        stud.setNavn(navn);
        return stud;
    }

    public View saveStudent() {
        Student stud = createStudObject();
        if (stud.getStudentNr() > 0) studEjb.update(stud);
        else studEjb.insert(stud);
        // conv.end();
        return View.studenter;
    }

    public View deleteStudent() {
        Student stud = createStudObject();
        studEjb.delete(stud);
        conv.end();

        return View.studenter;
    }
}
